var request = require("../../utils/request.js");
// 获取底部自定义菜单
const app = getApp();
let param_url,param_data;
//下拉菜单获取应用实例


Page({

  /**
   * 页面的初始数据
   */
  data: {
    course: [],
    panelArr: [],
    type: null,
    panelShow: false,
    contentHeight:0,
    pageNum:1,
    flag:true
  },

  panelShow: function(e) {
    var that = this;
    var bool = that.data.panelShow ? false : true
    var id = e.currentTarget.dataset.id;
    if (that.data.type != id) {
      bool = true;
    }
    console.log(bool);
    var arr = [];
    switch (parseInt(id)) {
      case 0:
        arr = ["分类一", "分类二", "分类三"]
        break;
      case 1:
        arr = ["推荐排序", "销量排序", "好评排序"]
        break;
      case 2:
        arr = ["筛选一", "筛选二", "筛选三"]
        break;
    }

    that.setData({
      type: id,
      panelShow: bool,
      panelArr:arr
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    this.init();
    console.log(options);

    var id = parseInt(options.id);
    var type = options.type;

    console.log(type)
    var url = '';
    var data = {}
    if (type == 0) {
      url = "/api/Course/GetCoursesByType";
      data['type'] = id;
      var title = '';
      switch (id) {
        case 0:
          title = "免费专区"
          break;
        case 3:
          title = "限时优惠"
          break;
        case 4:
          title = "团购专区";
          break;
      }
      wx.setNavigationBarTitle({
        title: title,
      })
    } else if (type == 1) {
      url = "/api/Course/GetCoursesByCategory";
      data['category'] = id;
      switch (id) {
        case 1:
          title = "沟通技巧"
          break;
        case 2:
          title = "亲子教育"
          break;
        case 3:
          title = "型享生活"
          break;
        case 4:
          title = "学生课堂"
          break;
      }
      wx.setNavigationBarTitle({
        title: title,
      })
    } else {
      switch (id) {
        case 0:
          url = "/api/Course/GetCoursesByHot";
          title = "人气课程"
          break;
        case 1:
          url = "/api/Course/GetCoursesByOnlineTimeNear";
          title = "最新上线"
          break;
        case 2:
          title = "实用技巧"
          break;
        case 3:
          title = "更多精选"
          break
      }
      wx.setNavigationBarTitle({
        title: title,
      })
    }
    param_url = url;
    param_data = data;
    this.getList(param_url, param_data);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  getList(url, data) {
    var that = this;

    data['pageIndex'] = that.data.pageNum;

    console.log(url,data);

    request.request(url, data).then(function(res) {

      console.log(res)
      var result = res.data;

      wx.showLoading({
        title: '加载中',
      })

      if (result.status == 1) {

        for (var i = 0; i < result.data.length; i++) {
          var pic = result.data[i]['Pic'].split(',')

          for (var j in pic) {
            pic[j] = "http://qbonbon.qclub-online.com/_Document/Image/" + pic[j]

          }

          result.data[i]['Pic'] = pic[0];

        }

        var data = that.data.course;

        if(result.data.length > 0){
            for(var i in result.data){
              data.push(result.data[i]);
            }
        }
        else{
          
          if(that.data.pageNum == 0){
            data = result.data;
          }else{
            that.setData({
              flag:false
            })
          }
        }

        that.setData({
          "course": data
        })
        wx.hideLoading();
      }
    });
  },
  detail: function(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/course/detail?id=' + id,
    })
  },
  init:function(){
    var sheight = this.getDomHeight();
  },
  getDomHeight:function() {
    var that = this;
    var query = wx.createSelectorQuery();
    query.select(".swiper").boundingClientRect()
    query.exec((res) => {
      var height = that.data.contentHeight;
      var contentHeight = res[0].height; // 获取list高度
      that.setData({
        contentHeight: height + contentHeight
      })
    })

    var query = wx.createSelectorQuery();
    query.select(".order-menu").boundingClientRect()
    query.exec((res) => {
      var height = that.data.contentHeight;
      var contentHeight = res[0].height; // 获取list高度

      wx.getSystemInfo({
        success: function(res) {
          contentHeight = res.windowHeight - height - contentHeight

          that.setData({
            contentHeight: contentHeight
          })
        },
      })
    })

  },
  stopAction: function (e) {
    return false;
  },
  //关闭弹窗方法
  closeBtn: function (e) {
    this.setData({
      panelShow: false
    })
  },

  //pageUp分页
  pageUp:function(){
    var that = this;
    console.log("分页方法", this.data.flag);
    var pageNum = this.data.pageNum;
    pageNum++;
    this.setData({
      pageNum: pageNum
    });
    if(that.data.flag){
      console.log(111);
      that.getList(param_url, param_data);
    }
  }
  
})